/**
* <p>Title: TestLinkUtil.java</p>
* <p>Description: </p>
* <p>Company: www.huoyunren.com</p>
* @author duanxiangyun
* @date 2016年1月8日
* @version 1.0
*/
package com.smarttest.common.util;

import java.net.MalformedURLException;
import java.net.URL;

import br.eti.kinoshita.testlinkjavaapi.TestLinkAPI;
import br.eti.kinoshita.testlinkjavaapi.model.Build;
import br.eti.kinoshita.testlinkjavaapi.model.TestPlan;
import br.eti.kinoshita.testlinkjavaapi.util.TestLinkAPIException;

public class TestLinkUtil {
	private static final String api_url = "http://172.22.0.20/testlink/lib/api/xmlrpc/v1/xmlrpc.php";
	private static final String api_key = "c390e34acb162f46b3cf180246f12b25";
	private static TestLinkAPI api = TestLinkUtil.getAPI(api_url,api_key);
	
	public static TestLinkAPI getApi() {
		return api;
	}

	/**
	 * 获得testlinkAPI
	 * @param apiurl testlinkapi的url链接
	 * @param userkey 用户的devkey
	 * @return
	 */
	public static TestLinkAPI getAPI(String apiurl, String userkey) {
		TestLinkAPI api = null;
        try {
            api = new TestLinkAPI(new URL(apiurl), userkey);
        } catch (TestLinkAPIException te) {
        	te.printStackTrace();
        } catch (MalformedURLException mue) {
        	mue.printStackTrace();
        }
        return api;
	}
	
	/**
	 * 获得最新的版本id
	 * @param testapi TestLinkAPI
	 * @param tp TestPlan
	 * @return int 最新的版本id
	 */
	public static int getNewestBuildId (TestLinkAPI testapi, TestPlan tp) {
		Build[] bs = testapi.getBuildsForTestPlan(tp.getId());
		int newestid = 0;
		int tmpid;
		for (Build b :bs) {
			tmpid = b.getId();
			if (newestid < tmpid) {
				newestid = tmpid;
			}
		}
		return newestid;
	}
	
//	public static void main(String[] args) {
//	    TestLinkAPI api = TestLinkUtil.getAPI(apiurl,apikey);
//		TestProject[] pro = api.getProjects();
//		for (int i = 0; i < pro.length; i++) {
//			
//			TestPlan[] plans = api.getProjectTestPlans(pro[i].getId());
//			Build[] builds = api.getBuildsForTestPlan(plans[0].getId());
//			TestCase[] cases = api.getTestCasesForTestPlan(plans[0].getId(), null, null, null, null, null, null, null, null, true,TestCaseDetails.SIMPLE);
//		}
//	}
	

//	/**
//     * 获得testlink自动化测试的测试集
//     * @return TestCase[]
//     */
//    public static TestCase[] getTestlinkCases () {	   
//    	TestCase[] tcs=api.getTestCasesForTestPlan(tl.getId(), null, null, null, null, null, null, null, null, true, TestCaseDetails.SIMPLE);
//    	return tcs;
//    }
//   /**
//    * 获得testlink对应runclass要运行的测试类
//    * @param tc
//    * @return String
//    */
//    public static String getRunTestFromTestlink (TestCase tc) {
//    	String customField = api.getTestCaseCustomFieldDesignValue(tc.getId(), null, 1, api.getTestProjectByName(projectName).getId(), custom_field, null).getValue();
//    	return customField;
//    }
//   /**
//    * 创建新的版本
//    * @return int
//    * @throws TestLinkAPIException
//    * @throws ParseException
//    */
//    public static int createBuild () throws TestLinkAPIException, ParseException {
//    	api.createBuild(tl.getId(), "dsp_interface_"+CommonUtils.getNow(), "此次构件针对于自动化测试,创建时间"+CommonUtils.getNow());
//    	int newestid = TestLinkUtil.getNewestBuildId(api, tl);
//    	return newestid;
//    }
}
